package com.xiaobug.demo.okgo;

import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ljb.commlibrary.http.HttpParams;
import com.ljb.commlibrary.http.HttpUtils;
import com.ljb.commlibrary.okgo.HttpCallBack;
import com.ljb.commlibrary.utils.L;

import com.xiaobug.baselibrary.base.BaseFragment;
import com.xiaobug.demo.R;
import com.xiaobug.demo.TestBean;
import com.xiaobug.demo.databinding.OkgoFragmentTestBinding;

/**
 * Created by ljb on 2018/5/3.
 */

public class OkgoTestFragment extends BaseFragment<OkgoFragmentTestBinding> {
    public class Presenter {
        public void onClick1(View view) {
            testGet();
        }

        public void onClick2(View view) {
            testPost();
        }

        public void onClick3(View view) {
            testCancle();
        }

        public void onClick4(View view) {
            finishCancle();
        }

    }

    String url = "http://app.shukuang.cn/ZHSW_APP_INTERFACE/system/factory/listByUserId.ajax";
    String url2 = "http://www.wanandroid.com/article/list/0/json";


    @Override
    protected int getRootID() {
        return R.layout.okgo_fragment_test;
    }


    @Override
    protected void initData(Bundle savedInstanceState, OkgoFragmentTestBinding binding) {
        binding.setP(new Presenter());
    }

    private void finishCancle() {
        for (int i = 0; i < 5; i++) {
            HttpUtils.getInstance().get(url2, null, String.class, new HttpCallBack<String>() {
                @Override
                public void onSucess(String s) {

                }

                @Override
                public void onError() {

                }
            });
        }
        getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        HttpUtils.getInstance().stop(url2);

    }

    /**
     * 测试取消方法
     */
    private void testCancle() {
        for (int i = 0; i < 5; i++) {
            String url = "http://www.wanandroid.com/article/list/0/json";
            HttpUtils.getInstance().get(url, null, String.class, new HttpCallBack<String>() {
                @Override
                public void onSucess(String s) {

                }

                @Override
                public void onError() {

                }
            });
        }

        HttpUtils.getInstance().cancleAll();
    }

    /**
     * 测试get方法
     */
    private void testGet() {
        HttpParams httpParams = new HttpParams();
        httpParams.put("factoryId", "0");
        httpParams.put("userId", "89");

        HttpUtils.getInstance().get(url, httpParams, TestBean.class, new HttpCallBack<TestBean>() {
            @Override
            public void onSucess(TestBean testBean) {
                L.e(new Gson().toJson(testBean));
            }

            @Override
            public void onError() {
                L.e("请求失败");
            }
        });
    }

    /**
     * 测试post方法
     */
    private void testPost() {
        HttpParams httpParams = new HttpParams();
        httpParams.put("factoryId", "0");
        httpParams.put("userId", "89");

        HttpUtils.getInstance().post(url, httpParams, TestBean.class, new HttpCallBack<TestBean>() {
            @Override
            public void onSucess(TestBean testBean) {
                L.e(new Gson().toJson(testBean));
            }

            @Override
            public void onError() {
                L.e("请求失败");
            }
        });
    }
}
